//
//  JWTUserHeaderView.m
//  JWT
//
//  Created by ndm on 17/1/19.
//  Copyright © 2017年 com.jwtsoft. All rights reserved.
//

#import "JWTUserHeaderView.h"
#import "JWTUserInfo.h"

@implementation JWTUserHeaderView

SHARED_INSTANCE_IMPLEMENTATION(JWTUserHeaderView);

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        if (_backImageView == nil) {
            UIImageView *headBackImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, UISCREEN_WIDTH, UISCREEN_HEIGHT*0.5)];
            [headBackImageView setImage:[UIImage imageNamed:@"UserInfoHeaderBackGround"]];
            [self addSubview:headBackImageView];
            _backImageView = headBackImageView;
        }
        
        if (_userHeadImageView == nil) {
            UIImageView *userHeadImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 8, 40, 40)];
            userHeadImageView.center = CGPointMake(UISCREEN_WIDTH*0.5, userHeadImageView.center.y);
            [userHeadImageView setImage:[UIImage imageNamed:@"my_vip_small"]];
            [self addSubview:userHeadImageView];
            userHeadImageView.hidden = YES;
            _userHeadImageView = userHeadImageView;
        }
        
        if (_userImageView == nil) {
            UIImageView *iconImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, _userHeadImageView.bottom-Cell_Margin, 80, 80)];
            iconImageView.layer.borderWidth = 0.5;
            iconImageView.layer.borderColor = RGB_LightGrayBackGround.CGColor;
            iconImageView.layer.cornerRadius = iconImageView.height*0.5;
            iconImageView.layer.masksToBounds = YES;
            iconImageView.center = CGPointMake(UISCREEN_WIDTH*0.5, iconImageView.center.y);
            iconImageView.layer.borderWidth  = 0.5;
            [iconImageView setImage:[UIImage imageNamed:@"my_user_default"]];
            [self addSubview:iconImageView];
            _userImageView = iconImageView;
        }
        
        if (_userSexImageView == nil) {
            UIImageView *userSexImageView = [[UIImageView alloc] initWithFrame:CGRectMake(_userImageView.left-Cell_Margin, _userImageView.bottom+Cell_Margin*2, 16, 16)];
            if ([APPDELEGATE.userInfo.gender integerValue]== UserSexMale) {
                [userSexImageView setImage:[UIImage imageNamed:@"my_gender_male"]];
            }else{
                [userSexImageView setImage:[UIImage imageNamed:@"my_gender_female"]];
            }
            [self addSubview:userSexImageView];
            _userSexImageView = userSexImageView;
        }
        
        if (_userNameLabel == nil) {
            UILabel *myNameLabel = [[UILabel alloc] init];
            myNameLabel.frame = CGRectMake(_userSexImageView.right+Cell_Margin, _userImageView.bottom+8, UISCREEN_WIDTH-_userSexImageView.right-2*Cell_Margin, 24);
            myNameLabel.font = Text_Font16;
            myNameLabel.textColor = RGB_WhiteBackGround;
            myNameLabel.textAlignment = NSTextAlignmentLeft;
//            myNameLabel.text = @"你懂吗";
            [self addSubview:myNameLabel];
            _userNameLabel = myNameLabel;
        }
        
        _userSexImageView.center = CGPointMake(_userSexImageView.center.x, _userNameLabel.center.y);
        
        if (_userDetailLabel == nil) {
            UILabel *myMobileLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.left+Cell_Margin, _userNameLabel.bottom+Cell_Margin*0.5, UISCREEN_WIDTH-2*Cell_Margin, _userNameLabel.height)];
            myMobileLabel.font = Text_Font14;
            myMobileLabel.textAlignment = NSTextAlignmentCenter;
            myMobileLabel.textColor = RGB_WhiteBackGround;
//            myMobileLabel.text = @"2017-02 入团";
            [self addSubview:myMobileLabel];
            _userDetailLabel = myMobileLabel;
        }
        
        if (_danceImageView == nil) {
            UIImageView *danceImageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.left, _userDetailLabel.bottom+Cell_Margin*2, UISCREEN_WIDTH, UISCREEN_HEIGHT*0.2)];
            danceImageView.contentMode = UIViewContentModeScaleAspectFit;
            [danceImageView setImage:[UIImage imageNamed:@"UserInfoHeader_dancing"]];
            [self addSubview:danceImageView];
            _danceImageView = danceImageView;
        }
        
        self.frame = CGRectMake(self.left, self.top, UISCREEN_WIDTH, _danceImageView.bottom+8);
        self.backImageView.frame = self.frame;
        
    }
    return self;
}

- (void)setUserInfo:(JWTUserInfo *)userInfo {
    _userInfo = userInfo;
    
    [self.userImageView sd_setImageWithURL:[NSURL URLWithString:userInfo.icon] placeholderImage:nil];
    if ([userInfo.cardname isEqualToString:@""]||[userInfo.cardname isEqualToString:@"非会员"]) {
        self.userHeadImageView.hidden = YES;
    }else{
        self.userHeadImageView.hidden = NO;
    }
    self.userNameLabel.text = userInfo.name;
}

@end
